package com.it.water.controller.HttpController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.water.common.Result;
//import com.it.water.config.WebSocketConfig;
import com.it.water.entity.MonitoringDevices;
import com.it.water.service.IMonitoringDevicesService;
import com.it.water.service.impl.MonitoringDevicesService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 设备信息控制器
 *
 * @author <a href="mailto:2038322151@qq.com">Miraitowa_zcx</a>
 * @author yjh
 * @author HelloWorld505
 * @version 1.0
 * @since 2024-03-18
 */
@Slf4j
@RestController
@RequestMapping("/devices")
@CrossOrigin
public class MonitoringDevicesController {

    /**
     * 设备信息服务
     */
    private final IMonitoringDevicesService monitoringDevicesService;

    /**
     * 注入设备信息服务
     *
     * @param monitoringDevicesService 设备信息服务
     */
    @Autowired
    public MonitoringDevicesController(MonitoringDevicesService monitoringDevicesService) {
        this.monitoringDevicesService = monitoringDevicesService;
    }

    //  @Autowired
    //  private WebSocketConfig.DataUpdateService dataUpdateService;

    /**
     * 设备信息分页查询
     *
     * @param page     分页页码
     * @param pageSize 分页大小
     * @param name     查询条件
     * @return 分页结果
     */
    @GetMapping("/page")
    public Result<Page<MonitoringDevices>> page(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam(required = false, defaultValue = "") String name) {
        log.info("page = {},pageSize = {},name = {}", page, pageSize, name);

        // 构造分页构造器
        Page<MonitoringDevices> pageInfo = new Page<>(page, pageSize);
        // 设置页数
        pageInfo.setCurrent(page);
        // 设置每页显示的记录数
        pageInfo.setSize(pageSize);
        // 构造条件构造器
        QueryWrapper<MonitoringDevices> queryWrapper = new QueryWrapper<>();
        // 添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name), "devicename", name);
        // 添加排序条件
        queryWrapper.orderByDesc("id");
        // 设置总记录数
        pageInfo.setTotal(monitoringDevicesService.count(queryWrapper));
        // 执行查询
        monitoringDevicesService.page(pageInfo, queryWrapper);

        //   dataUpdateService.sendUpdate();

        return Result.success(pageInfo);
    }

    /**
     * 新增设备信息
     *
     * @param monitoringDevices 设备信息
     * @return 新增结果
     */
    @PostMapping("/save")
    public Result<String> save(@RequestBody MonitoringDevices monitoringDevices) {
        log.info("新增设备，设备信息：{}", monitoringDevices.toString());
        monitoringDevicesService.save(monitoringDevices);
        return Result.success();
    }

    /**
     * 修改设备信息
     *
     * @param monitoringDevices 设备信息
     * @return 修改结果
     */
    @PutMapping("/update/{id}")
    public Result<String> update(@RequestBody MonitoringDevices monitoringDevices) {
        log.info(monitoringDevices.toString());
        monitoringDevicesService.updateById(monitoringDevices);
        return Result.success("设备信息修改成功");
    }

    /**
     * 根据id查询设备信息
     *
     * @param id 设备id
     * @return 设备信息
     */
    @GetMapping("/{id}")
    public Result<MonitoringDevices> getById(@PathVariable Integer id) {
        log.info("根据id查询设备信息...");
        MonitoringDevices monitoringDevices = monitoringDevicesService.getById(id);
        if (monitoringDevices != null) {
            return Result.success(monitoringDevices);
        }
        return Result.error("没有查询到对应设备信息");
    }

    /**
     * 根据id删除设备信息
     *
     * @param id 设备id
     * @return 删除结果
     */
    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Integer id) {
        log.info("根据id删除设备信息...");
        monitoringDevicesService.removeById(id);
        return Result.success("删除设备成功");
    }
}
